package MainProgram;

import Algorithms.NeuralSimulatedAnnealing;
import NeuronNetworkLibrary.Network;
import java.util.Random;

/**
 * Main class.
 *
 * @author Zbyszko
 */
public class SAMain {

    public final static int EPOCH = 1000;
    public final static double ERROR = 0.000001;

    public static void main(String[] args) {

        //SinusCreator data = new SinusCreator(10);
        SimpleDataCreator data = new SimpleDataCreator();
        // Create and build the network.
        Network network = new Network(data.getTrainingSet(), data.getDesiredOutput(),
                new int[]{4}, new String[]{"sigmo"}, "linear");
        NeuralSimulatedAnnealing NSA = new NeuralSimulatedAnnealing(network, 100, 25, 2);


        int i = 0;
        while (i <= EPOCH) {
            // Use Simulated Annealing for training network.
            NSA.annealNetwork();
            i++;
        }

        // Print and show the result on the plot.
        Plot plot = new Plot(
                "Neural Network", //window title 
                "", //plot title
                data.getTrainingSet(), //training set 
                data.getDesiredOutput(), //desired output
                printFinalResults(network) //obtained result
                );
        plot.setVisible(true);
    }

    private static double[][] printFinalResults(Network network) {

        int patternNumber = network.getNumberOfPatterns();
        int patternLength = network.getOutputLayer().size();

        double[][] obtainedResults = new double[patternNumber][patternLength];
        for (int i = 0; i < network.getNumberOfPatterns(); i++) {
            network.calculateNetwork(i);

            System.out.println("\nPattern nr: " + (i + 1));
            for (int j = 0; j < network.getOutputLayer().size(); j++) {
                System.out.println(network.getOutputLayer().get(j));
                obtainedResults[i][j] = network.getOutputLayer().get(j).getCalculatedOutput();
            }
        }
        System.out.println("\nMSE: " + network.getMSE());

        return obtainedResults;
    }
}
